package com.qianxun.export.headler;


import com.qianxun.framework.common.util.ErrorUtil;
import com.qianxun.framework.common.util.Md5Util;
import com.qianxun.framework.web.error.ServiceInterruptionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Iterator;

import java.util.List;
import java.util.Set;
import com.qianxun.module.base.constant.Result;

/**
 * Controller 全局异常处理类
 * 若需要针对某一类或某一个controller处理，请设置@ControllerAdvice
 * @author: huwei
 * @date:   2019/10/17 14:38
 * @version:  1.0.0
*/
@Slf4j
@ResponseBody
@ControllerAdvice()
public class GlobalExceptionHandler {
	/**
	 * 未知异常处理
	 */
	@ExceptionHandler(value = {Throwable.class ,Exception.class ,RuntimeException.class})
	public Result unknown(Throwable e) {
		log.error(new StringBuffer("未知异常: ").append(e.getMessage()).toString() ,e);

		return Result.fail(500 ,e.getMessage());
	}

	/**
	 * 空指针异常
	 */
	@ExceptionHandler(value = {NullPointerException.class})
	public Result nullPointException(NullPointerException e){
		log.error(new StringBuffer("空指针异常: ").append(e.getMessage()).toString(),e);
		return Result.fail(500 ,e.getMessage());
	}

	/**
	 * 下标越界异常
	 */
	@ExceptionHandler(value = {IndexOutOfBoundsException.class})
	public Result indexOutOfBoundsException(IndexOutOfBoundsException e){
		log.error(new StringBuffer("下标越界异常: ").append(e.getMessage()).toString() ,e);
		return Result.fail(500 ,e.getMessage());
	}

	/**
	 * 业务中断异常
	 * @param e 异常对象
	 * @return
	 */
	@ExceptionHandler(value = {ServiceInterruptionException.class})
	public Result serviceInterruptionException(ServiceInterruptionException e){
		log.info(new StringBuffer("业务中断异常: ").append(e.getMessage()).toString());
		if(e.isSuccess()){
			return Result.success(e.getData() ,e.getMessage());
		}
		return Result.fail(e.getCode() ,e.getMessage() ,e.getData());
	}

	/**
	 * 绑定参数异常
	 * @param bindException 错误信息
	 * @return 返回结果
	 */
	@ExceptionHandler(value = {BindException.class})
	public Result BindException(BindException bindException){
		StringBuffer errorMessage = new StringBuffer();
		List<ObjectError> objectErrorList = bindException.getAllErrors();
		objectErrorList.forEach(objectError -> {
			errorMessage.append(objectError.getDefaultMessage()).append("; ");

		});
		return Result.fail(400,"参数错误: " + errorMessage.toString());
	}

	/**
	 * 缺少请求参数处理
	 */
	@ExceptionHandler(value = {MissingServletRequestParameterException.class})
	public Result missingServletRequestParameterException(MissingServletRequestParameterException ex) {
		return Result.fail(400 ,new StringBuffer("缺少参数: ").append(ex.getParameterName()).toString());
	}
	/**
	 * 违反约束处理
	 */
	@ExceptionHandler(ConstraintViolationException.class)
	public Result constraintViolationException(ConstraintViolationException e) {
		Set<ConstraintViolation<?>> violations =  e.getConstraintViolations();
		Iterator<ConstraintViolation<?>> iter = violations.iterator();
		StringBuffer sb = new StringBuffer();
		while(iter.hasNext()) {
			ConstraintViolation<?> violation = iter.next();
			sb.append(violation.getMessage()).append(",");
		}
		return Result.fail(400 ,sb.toString());
	}
	/**
	 * 请求参数类型转换错误异常处理
	 */
	@ExceptionHandler(MethodArgumentTypeMismatchException.class)
	public Result numberFormatException(MethodArgumentTypeMismatchException ex) {
		return Result.fail(400,new StringBuffer("参数格式不匹配: ").append(ex.getName()).toString());
	}
}
